package com.aaa.system.controller.api;

import com.aaa.entity.User;
import com.aaa.security.annotation.HasPermission;
import com.aaa.security.annotation.MyLog;
import com.aaa.system.service.UserService;
import com.aaa.util.UploadUtil;
import com.aaa.util.UploadUtil1;
import com.aaa.vo.FileVo;
import com.aaa.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author JYH
 * @version 1.0
 * @date 2023/2/27 21:14
 */
@Api(value = "/system/api/user", tags = {"用户控制"})
@RestController
@RequestMapping("/system/api/user")
public class ApiUserController {
    @Autowired
    private UserService userService;

    /**
     * 模糊查询
     * @param currentPage
     * @param pageSize
     * @param user
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "currentPage", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "User", name = "user", value = "", required = true)
    })
    @ApiOperation(value = "模糊查询", notes = "模糊查询")
    @MyLog(title="查询用户")
    @RequestMapping("select")
    @HasPermission(value = "user:search")
    public Result list(@RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "5") int pageSize, @RequestBody User user){
        return userService.selectAll(currentPage,pageSize,user);
    }


    /**
     * 添加用户
     * @param user
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "User", name = "user", value = "", required = true)
    })
    @ApiOperation(value = "添加用户", notes = "添加用户", httpMethod = "POST")
    @MyLog(title="添加用户")
    @PostMapping("insert")
    @HasPermission(value = "user:add")
    public Result insert(@RequestBody User user){
        Result insert = userService.insert(user);
        return insert;
    }



    /**
     * 修改用户
     * @param user
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "User", name = "user", value = "", required = true)
    })
    @ApiOperation(value = "修改用户", notes = "修改用户", httpMethod = "POST")
    @MyLog(title="修改用户")
    @PostMapping("update")
    @HasPermission(value = "user:modify")
    public Result update(@RequestBody User user){
        Result update = userService.update(user);
        return update;
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "userId", value = "", required = true)
    })
    @ApiOperation(value = "删除用户", notes = "删除用户", httpMethod = "GET")
    @MyLog(title="删除用户")
    @GetMapping("delete")
    public Result delete(Integer userId){
        Result delete = userService.delete(userId);
        return delete;
    }

    /**
     * 添加角色
     * @param userId
     * @param roleId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "userId", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "roleId", value = "", required = true)
    })
    @ApiOperation(value = "添加角色", notes = "添加角色", httpMethod = "POST")
    @MyLog(title="添加角色")
    @PostMapping("insertByRole")
    public Result insertByRole(Integer userId,Integer roleId){
        Result result = userService.insertByRole(userId, roleId);
        return result;
    }

    /**
     *头像上传
     * @param file
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "file", name = "file", value = "", required = true)
    })
    @ApiOperation(value = "头像上传", notes = "头像上传")
    @MyLog(title="头像上传")
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(MultipartFile file)throws Exception {
        String path = UploadUtil.ossUpload(file);
        return new Result(2000, "添加成功", path);
    }
    /**
     *头像上传
     * @param file
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "FileVo", name = "file", value = "", required = true)
    })
    @ApiOperation(value = "头像上传", notes = "头像上传")
    @MyLog(title="头像上传")
    @RequestMapping("/upload02")
    @ResponseBody
    public Result upload02(FileVo file)throws Exception {
        String path = UploadUtil1.ossUpload(file.getImgFile());
        return new Result(2000, "添加成功", path);
    }


    /**
     * 查询所有角色和用户具有的角色id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "userId", value = "", required = true)
    })
    @ApiOperation(value = "查询所有角色和用户具有的角色id", notes = "查询所有角色和用户具有的角色id", httpMethod = "GET")
    @MyLog(title="查询所有角色")
    @GetMapping("/list")
    public Result list(Integer userId){
        return userService.findByUserId(userId);
    }

    /**
     * 分配角色
     * @param userId
     * @param roleIds
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "userId", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer[]", name = "roleIds", value = "", required = true)
    })
    @ApiOperation(value = "分配角色", notes = "分配角色", httpMethod = "POST")
    @MyLog(title="查询用户的角色id")
    @PostMapping("qr")
    @HasPermission(value = "user:impower")
    public Result qr(Integer userId,Integer[] roleIds){

        return  userService.qr(userId,roleIds);
    }
}
